package com.example.yls.drawcanvar;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by yls on 2019/5/15.
 */

public class PaintView extends View {
    public static final int RECT=1;
    public static final int CIRCLE=2;
    public static final int PEN=0;

    private Point rectLeftTop=new Point();
    private Point rectRightBottom=new Point();

    private Point cicleCenter=new Point();
    private float radius;

    private int mode = PEN;
    private Paint paint=new Paint();
    private Path path=new Path();

    public PaintView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        switch (mode){
            case PEN:
                canvas.drawPath(path,paint);
                break;
            case RECT:
                canvas.drawRect(rectLeftTop.x,rectLeftTop.y,rectRightBottom.x,rectRightBottom.y,paint);
                break;
            case CIRCLE:
                canvas.drawCircle(cicleCenter.x,cicleCenter.y,radius,paint);
                break;
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("PaintView","x = "+event.getX()+" y = "+event.getY());

        switch (mode){
            case PEN:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:  //按下
                        path.moveTo(event.getX(),event.getY());
                        break;
                    case MotionEvent.ACTION_MOVE: //移动
                        path.lineTo(event.getX(),event.getY());
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:  //松开
                        break;
                }

            case RECT:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:  //按下
                         rectLeftTop.x= (int) event.getX();
                         rectLeftTop.y= (int) event.getY();
                         break;
                    case MotionEvent.ACTION_MOVE: //移动
                         rectRightBottom.x= (int) event.getX();
                         rectRightBottom.y= (int) event.getY();
                         invalidate();
                        break;
                    case MotionEvent.ACTION_UP:  //松开
                        break;
                }
            case CIRCLE:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:  //按下
                        cicleCenter.x= (int) event.getX();
                        cicleCenter.y= (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE: //移动
                        //sqrt:开平方  pow：平方
                        radius= (float) Math.sqrt(Math.pow(event.getX()-cicleCenter.x,2)
                                +Math.pow(event.getY()-cicleCenter.y,2));
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:  //松开
                        break;
                }
        }
        return true;
    }

    public void setPaintColor(int paintColor) {
        paint.setColor(paintColor);
    }

    public void setPaintMode(int mode) {
        this.mode=mode;
    }
}